/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.web;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.gis.eemc.dao.DepartmentDAO;
import net.gis.eemc.dao.DepartmentDAOImpl;
import net.gis.eemc.dao.FacultyDAO;
import net.gis.eemc.dao.FacultyDAOImpl;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.Faculty;

/**
 *
 * @author Sergey
 */
@ManagedBean(name = "addDepartmentBean")
@SessionScoped
public class AddDepartmentBean {

    private Map<Integer, Faculty> facultyMap = null;
    private Integer selectedFaculty = null;
    private String title = null;
    private String shortName = null;
    private DepartmentDAO depDAO = null;
    private FacultyDAO facDAO = null;
    private List<SelectItem> selectItems = null;

    private void fillSelectItems() {
        selectItems = new ArrayList<SelectItem>();
        facultyMap = new HashMap<Integer, Faculty>();
        for (Faculty fac : facDAO.listFaculty()) {
            facultyMap.put(fac.getIdfaculty(), fac);
            selectItems.add(new SelectItem(fac.getIdfaculty(), fac.getShortName()));
        }
    }

    /** Creates a new instance of DepartmentBean */
    public AddDepartmentBean() {
        depDAO = new DepartmentDAOImpl();
        facDAO = new FacultyDAOImpl();

        fillSelectItems();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public List<SelectItem> getSelectItems() {
        return selectItems;
    }

    public Integer getSelectedFaculty() {
        return selectedFaculty;
    }

    public void setSelectedFaculty(Integer faculty) {
        this.selectedFaculty = faculty;
    }

    public void Save() {
        if (title != null && shortName != null && selectedFaculty != null) {
            depDAO.addDepartment(new Department(facultyMap.get(selectedFaculty), title, shortName));
        }
    }
}
